cleanup datum lookup & constants (#962)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Wed, 28 Dec 2022 23:41:04 +0000 (16:41 -0700)
committerGitHub <noreply@github.com>
Wed, 28 Dec 2022 23:41:04 +0000 (16:41 -0700)
* cleanup datum lookup

* use constexpr for datum contstants

defs.h
exif.cc
garmin_txt.cc
jeeps/gpsmath.h
main.cc
nmea.cc
ozi.cc
parse.cc
unicsv.cc
xcsv.cc
xcsv.h

diff --git a/defs.h b/defs.h
index 52524b93e7d870010bbd035633d12a94eb3874bd..c6778ee7f2b9826b5ce9825aa9d42cb56e42dbaa 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -129,6 +129,9 @@ constexpr double KNOTS_TO_MPS(double a)  {return a * kMPSPerKnot;}
 #define CENTI_TO_MICRO(t) ((t) * 10000) /* Centiseconds to Microseconds */
 #define MICRO_TO_CENTI(t) ((t) / 10000) /* Centiseconds to Microseconds */
 
+constexpr int kDatumOSGB36 = 86; // GPS_Lookup_Datum_Index("OSGB36")
+constexpr int kDautmWGS84 = 118; // GPS_Lookup_Datum_Index("WGS 84")
+
 /* Pathname separator character */
 #if __WIN32__
 #  define GB_PATHSEP '\\'
@@ -1154,9 +1157,6 @@ enum grid_type {
 #define GRID_INDEX_MIN grid_lat_lon_ddd
 #define GRID_INDEX_MAX grid_swiss
 
-#define DATUM_OSGB36   86
-#define DATUM_WGS84    118
-
 /* bit manipulation functions (util.c) */
 
 char gb_getbit(const void* buf, uint32_t nr);
@@ -1199,8 +1199,4 @@ int color_to_bbggrr(const char* cname);
 #define unknown_alt    -99999999.0
 #define unknown_color  -1
 
-// TODO: this is a (probably temporary) shim for the C->QString conversion.
-// It's here instead of gps to avoid C/C++ linkage issues.
-int32_t GPS_Lookup_Datum_Index(const QString& n);
-
 #endif // DEFS_H_INCLUDED_
diff --git a/exif.cc b/exif.cc
index 429e50938b829371b4241ee8fcb1ccad1e0307c6..8c41c45faecd557e06f9b5b72dc0a340f3266b2b 100644 (file)
--- a/exif.cc
+++ b/exif.cc
@@ -826,7 +826,7 @@ ExifFormat::exif_waypt_from_exif_app(ExifApp* app) const
     if (idatum < 0) {
       fatal(MYNAME ": Unknown GPSMapDatum \"%s\"!\n", datum);
     }
-    if (idatum != DATUM_WGS84) {
+    if (idatum != kDautmWGS84) {
       GPS_Math_WGS84_To_Known_Datum_M(wpt->latitude, wpt->longitude, 0.0,
                                       &wpt->latitude, &wpt->longitude, &alt, idatum);
     }
index 256d49976dd513f5fc333e3a7ee6002eade06d14..46d4cd3f1c23812219d8643265cb44aaa4e7f123 100644 (file)
@@ -206,7 +206,7 @@ convert_datum(const Waypoint* wpt, double* dest_lat, double* dest_lon)
 {
   double alt;
 
-  if (datum_index == DATUM_WGS84) {
+  if (datum_index == kDautmWGS84) {
     *dest_lat = wpt->latitude;
     *dest_lon = wpt->longitude;
   } else GPS_Math_WGS84_To_Known_Datum_M(wpt->latitude, wpt->longitude, 0.0,
@@ -776,10 +776,10 @@ garmin_txt_wr_init(const QString& fname)
 
   switch (grid_index) {
   case grid_bng: /* force datum to "Ord Srvy Grt Britn" */
-    datum_index = DATUM_OSGB36;
+    datum_index = kDatumOSGB36;
     break;
   case grid_swiss: /* force datum to WGS84 */
-    datum_index = DATUM_WGS84;
+    datum_index = kDautmWGS84;
     break;
   default:
     datum_index = gt_lookup_datum_index(datum_str, MYNAME);
index 788e3d4bce8687fe17e7bdce6eb50644867f5459..f3915ad57571d1b9e3bb21456ebd2ec500679c83 100644 (file)
                                  double lambda0, double E0, double N0);
 
   int32 GPS_Lookup_Datum_Index(const char* n);
+  int32 GPS_Lookup_Datum_Index(const QString& n);
   const char* GPS_Math_Get_Datum_Name(int datum_index);
 
 #endif
diff --git a/main.cc b/main.cc
index 02c274687e63e354dd1c1ae71ad4e56a84c7af3b..52bcb902811cde425f5cc923f33e4369568a4ff1 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -17,6 +17,7 @@
 
  */
 
+#include <cassert>                    // for assert
 #include <clocale>                    // for setlocale, LC_NUMERIC, LC_TIME
 #include <csignal>                    // for signal, SIGINT, SIG_ERR
 #include <cstdio>                     // for printf, fflush, fgetc, fprintf, stderr, stdin, stdout
@@ -50,6 +51,7 @@
 #include "format.h"                   // for Format
 #include "gbversion.h"                // for VERSION_SHA
 #include "inifile.h"                  // for inifile_done, inifile_init
+#include "jeeps/gpsmath.h"            // for GPS_Lookup_Datum_Index
 #include "session.h"                  // for start_session, session_exit, session_init
 #include "src/core/datetime.h"        // for DateTime
 #include "src/core/file.h"            // for File
@@ -718,6 +720,9 @@ main(int argc, char* argv[])
     global_opts.inifile = inifile_init(QString(), MYNAME);
   }
 
+  assert(GPS_Lookup_Datum_Index("OSGB36") == kDatumOSGB36);
+  assert(GPS_Lookup_Datum_Index("WGS 84") == kDautmWGS84);
+
   Vecs::Instance().init_vecs();
   FilterVecs::Instance().init_filter_vecs();
   session_init();
diff --git a/nmea.cc b/nmea.cc
index dcdf6c0d62d0f9d181c89fce5e7e766da4263a78..431e505bf8521bab711e454c34e91ae31712233a 100644 (file)
--- a/nmea.cc
+++ b/nmea.cc
@@ -191,7 +191,7 @@ NmeaFormat::nmea_add_wpt(Waypoint* wpt, route_head* trk) const
   // This also indicates to nmea_release_wpt that ownership has been
   // transferred to either the global_waypoint_list or global_track_list.
   wpt->extra_data = nullptr;
-  if (datum != DATUM_WGS84) {
+  if (datum != kDautmWGS84) {
     double lat, lon, alt;
     GPS_Math_Known_Datum_To_WGS84_M(
       wpt->latitude, wpt->longitude, 0,
@@ -221,7 +221,7 @@ NmeaFormat::rd_init(const QString& fname)
 {
   curr_waypt = nullptr;
   last_waypt = nullptr;
-  datum = DATUM_WGS84;
+  datum = kDautmWGS84;
   had_checksum = false;
 
   CHECK_BOOL(opt_gprmc);
diff --git a/ozi.cc b/ozi.cc
index a6aed2d883033dcdb9487edf68dcaab4784283d7..658a8a26be12864f5f51eecda066ee421b2846df 100644 (file)
--- a/ozi.cc
+++ b/ozi.cc
@@ -215,7 +215,7 @@ ozi_set_time_str(const QString& str, Waypoint* waypointp)
 static void
 ozi_convert_datum(Waypoint* wpt)
 {
-  if (datum != DATUM_WGS84) {
+  if (datum != kDautmWGS84) {
     double lat, lon, alt;
     GPS_Math_Known_Datum_To_WGS84_M(wpt->latitude, wpt->longitude, 0.0,
                                     &lat, &lon, &alt, datum);
index 62b099d6d334b5e354f7923cc68a767f78531201..e589e50792edbed854a8d4ac74ba8920e8447851 100644 (file)
--- a/parse.cc
+++ b/parse.cc
@@ -207,7 +207,7 @@ parse_coordinates(const char* str, int datum, const grid_type grid,
     break;
 
   case grid_bng:
-    datum = DATUM_WGS84;       /* fix */
+    datum = kDautmWGS84;       /* fix */
     format = "%2s %lf %lf%n";
     ct = sscanf(str, format,
                 map, &lx, &ly,
@@ -238,7 +238,7 @@ parse_coordinates(const char* str, int datum, const grid_type grid,
   case grid_swiss: {
     double east, north;
 
-    datum = DATUM_WGS84;       /* fix */
+    datum = kDautmWGS84;       /* fix */
     format = "%lf %lf%n";
     ct = sscanf(str, format,
                 &east, &north, &result);
@@ -265,7 +265,7 @@ parse_coordinates(const char* str, int datum, const grid_type grid,
     lon = -lon;
   }
 
-  if (datum != DATUM_WGS84) {
+  if (datum != kDautmWGS84) {
     double alt;
     GPS_Math_Known_Datum_To_WGS84_M(lat, lon, 0.0,
                                     &lat, &lon, &alt, datum);
index 5ed30e6c6771bfb45bcf4102a5b9b2d25bb2f39b..0f710b66606fb8135d7e07aba90ffc476249d533 100644 (file)
--- a/unicsv.cc
+++ b/unicsv.cc
@@ -596,15 +596,15 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
                         &wpt->latitude, &wpt->longitude, MYNAME);
       /* coordinates from parse_coordinates are in WGS84
          don't convert a second time */
-      src_datum = DATUM_WGS84;
+      src_datum = kDautmWGS84;
       break;
 
     case fld_bng:
-      parse_coordinates(value, DATUM_OSGB36, grid_bng,
+      parse_coordinates(value, kDatumOSGB36, grid_bng,
                         &wpt->latitude, &wpt->longitude, MYNAME);
       /* coordinates from parse_coordinates are in WGS84
          don't convert a second time */
-      src_datum = DATUM_WGS84;
+      src_datum = kDautmWGS84;
       break;
 
     case fld_bng_zone:
@@ -621,11 +621,11 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
       break;
 
     case fld_swiss:
-      parse_coordinates(value, DATUM_WGS84, grid_swiss,
+      parse_coordinates(value, kDautmWGS84, grid_swiss,
                         &wpt->latitude, &wpt->longitude, MYNAME);
       /* coordinates from parse_coordinates are in WGS84
          don't convert a second time */
-      src_datum = DATUM_WGS84;
+      src_datum = kDautmWGS84;
       break;
 
     case fld_swiss_easting:
@@ -1051,15 +1051,15 @@ UnicsvFormat::unicsv_parse_one_line(const QString& ibuf)
           fatal(MYNAME ": Unable to convert BNG coordinates (%s %.f %.f)!\n",
                 bng_zone, bng_easting, bng_northing);
       }
-      src_datum = DATUM_WGS84; /* don't convert afterwards */
+      src_datum = kDautmWGS84; /* don't convert afterwards */
     } else if ((swiss_easting != kUnicsvUnknown) && (swiss_northing != kUnicsvUnknown)) {
       GPS_Math_Swiss_EN_To_WGS84(swiss_easting, swiss_northing,
                                  &wpt->latitude, &wpt->longitude);
-      src_datum = DATUM_WGS84; /* don't convert afterwards */
+      src_datum = kDautmWGS84; /* don't convert afterwards */
     }
   }
 
-  if ((src_datum != DATUM_WGS84) &&
+  if ((src_datum != kDautmWGS84) &&
       (wpt->latitude != kUnicsvUnknown) && (wpt->longitude != kUnicsvUnknown)) {
     double alt;
     GPS_Math_Known_Datum_To_WGS84_M(wpt->latitude, wpt->longitude, 0.0,
@@ -1311,7 +1311,7 @@ UnicsvFormat::unicsv_waypt_disp_cb(const Waypoint* wpt)
   QString shortname = wpt->shortname;
   garmin_fs_t* gmsd = garmin_fs_t::find(wpt);
 
-  if (unicsv_datum_idx == DATUM_WGS84) {
+  if (unicsv_datum_idx == kDautmWGS84) {
     lat = wpt->latitude;
     lon = wpt->longitude;
     alt = wpt->altitude;
@@ -1712,7 +1712,7 @@ UnicsvFormat::wr_init(const QString& fname)
 
   memset(&unicsv_outp_flags, 0, sizeof(unicsv_outp_flags));
   unicsv_grid_idx = grid_unknown;
-  unicsv_datum_idx = DATUM_WGS84;
+  unicsv_datum_idx = kDautmWGS84;
   unicsv_fieldsep = kUnicsvFieldSep;
   unicsv_waypt_ct = 0;
 
@@ -1733,11 +1733,11 @@ UnicsvFormat::wr_init(const QString& fname)
     /* force datum to "Ord Srvy Grt Britn" / OSGB36 */
     /* ! ignore parameter "Datum" ! */
   {
-    unicsv_datum_idx = DATUM_OSGB36;
+    unicsv_datum_idx = kDatumOSGB36;
   } else if (unicsv_grid_idx == grid_swiss)
     /* ! ignore parameter "Datum" ! */
   {
-    unicsv_datum_idx = DATUM_WGS84;  /* internal, becomes CH1903 */
+    unicsv_datum_idx = kDautmWGS84;  /* internal, becomes CH1903 */
   } else {
     unicsv_datum_idx = gt_lookup_datum_index(opt_datum, MYNAME);
   }
diff --git a/xcsv.cc b/xcsv.cc
index c4f21ca9492d89948ab5619306cf7b8fa995b124..f062444a4251ba03f2e93e8c23d2ba9bf9aa79dd 100644 (file)
--- a/xcsv.cc
+++ b/xcsv.cc
@@ -515,7 +515,7 @@ XcsvFormat::xcsv_parse_val(const QString& value, Waypoint* wpt, const XcsvStyle:
     break;
   /* SPECIAL COORDINATES/GRID */
   case XcsvStyle::XT_MAP_EN_BNG:
-    parse_coordinates(s, DATUM_OSGB36, grid_bng,
+    parse_coordinates(s, kDatumOSGB36, grid_bng,
                       &wpt->latitude, &wpt->longitude, MYNAME);
     break;
   case XcsvStyle::XT_UTM_ZONE:
@@ -891,7 +891,7 @@ XcsvFormat::read()
         wpt_tmp->longitude = -wpt_tmp->longitude;
       }
 
-      if ((xcsv_file->gps_datum_idx > -1) && (xcsv_file->gps_datum_idx != gps_datum_wgs84)) {
+      if ((xcsv_file->gps_datum_idx > -1) && (xcsv_file->gps_datum_idx != kDautmWGS84)) {
         double alt;
         GPS_Math_Known_Datum_To_WGS84_M(wpt_tmp->latitude, wpt_tmp->longitude, 0.0,
                                         &wpt_tmp->latitude, &wpt_tmp->longitude, &alt, xcsv_file->gps_datum_idx);
@@ -902,7 +902,7 @@ XcsvFormat::read()
                                        &wpt_tmp->longitude,
                                        parse_data.utm_easting, parse_data.utm_northing,
                                        parse_data.utm_zone, parse_data.utm_zonec,
-                                       DATUM_WGS84);
+                                       kDautmWGS84);
       }
 
       if (parse_data.link_) {
@@ -1018,7 +1018,7 @@ XcsvFormat::xcsv_waypt_pr(const Waypoint* wpt)
     description = shortname;
   }
 
-  if ((xcsv_file->gps_datum_idx > -1) && (xcsv_file->gps_datum_idx != gps_datum_wgs84)) {
+  if ((xcsv_file->gps_datum_idx > -1) && (xcsv_file->gps_datum_idx != kDautmWGS84)) {
     double alt;
     GPS_Math_WGS84_To_Known_Datum_M(latitude, longitude, 0.0,
                                     &latitude, &longitude, &alt, xcsv_file->gps_datum_idx);
@@ -1863,7 +1863,6 @@ XcsvFormat::rd_init(const QString& fname)
   if (xcsv_file->gps_datum_idx < 0) {
     fatal(MYNAME ": datum \"%s\" is not supported.", qPrintable(datum_name));
   }
-  assert(gps_datum_wgs84 == GPS_Lookup_Datum_Index("WGS 84"));
 }
 
 void
@@ -1944,7 +1943,6 @@ XcsvFormat::wr_init(const QString& fname)
   if (xcsv_file->gps_datum_idx < 0) {
     fatal(MYNAME ": datum \"%s\" is not supported.", qPrintable(datum_name));
   }
-  assert(gps_datum_wgs84 == GPS_Lookup_Datum_Index("WGS 84"));
 }
 
 void
diff --git a/xcsv.h b/xcsv.h
index 32cd13c668243b121ce2951f349acd436395febe..e4bc620b722333ff0095964f2d2d25509dbe06cc 100644 (file)
--- a/xcsv.h
+++ b/xcsv.h
@@ -364,8 +364,6 @@ private:
     return (a / 86400.0) + 25569.0;
   }
 
-  static constexpr int gps_datum_wgs84 = 118; // GPS_Lookup_Datum_Index("WGS 84")
-
   /* Member Functions */
 
   static QDateTime yyyymmdd_to_time(const QString& s);